<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Adv extends Controller_Admin_Based {

    /**
     * 列出所有广告
     */
    public function action_list() {
        $name_template = '<a href="/admin/adv/list/{id}" title="点击修改">{name}</a>';
        $conditional_name_template = '<a href="/admin/adv/list/{id}" title="点击修改">[ {name} ]</a> ';
        $content_data['catalog_tree'] = Catehelp::get_left_tree($name_template, $conditional_name_template, array('table' => 'cheers_main_advtypes', 'id' => 'chr_advtypeid', 'name' => 'chr_advtypename'));

        if ($advtypeid = $this->request->param('id')) {
            $childid = Catehelp::get_childs($advtypeid, 'cheers_main_advtypes', 'chr_advtypeid');
        }
        if (isset($advtypeid)) {
            $count = ORM::factory('cheers_main_adv')
                    ->where('chr_advtypeid', 'in', $childid)
                    ->count_all();
        } else {
            $count = ORM::factory('cheers_main_adv')
                    ->count_all();
        }

        $pagination = Pagination::factory(
                        array(
                            'current_page' => array('source' => 'query_string', 'key' => 'page'),
                            'total_items' => $count,
                            'auto_hide' => 'FALSE',
                        )
        );
        $page_view = $pagination->render();
        if ($advtypeid = $this->request->param('id')) {
            $data = ORM::factory('cheers_main_adv')
                    ->where('chr_advtypeid', 'in', $childid)
                    ->order_by('chr_advsid', 'DESC')
                    ->limit($pagination->items_per_page)
                    ->offset($pagination->offset)
                    ->find_all()
                    ->as_array();
        } else {
            $data = ORM::factory('cheers_main_adv')
                    ->order_by('chr_advsid', 'DESC')
                    ->limit($pagination->items_per_page)
                    ->offset($pagination->offset)
                    ->find_all()
                    ->as_array();
        }
        $content = View::factory('admin/advert/adv_list', $content_data)
                        ->set('data', $data)
                        ->set('count', $count)
                        ->set('page_view', $page_view)->render();
        $this->template = View::factory('admin/template')
                ->set('content', $content);
    }

    /**
     * 列出广告位置
     */
    public function action_add() {
        $name_template = '<a href="/admin/adv/list/{id}" title="点击修改">{name}</a>';
        $conditional_name_template = '<a href="/admin/adv/list/{id}" title="点击修改">[ {name} ]</a>';
        $content_data['catalog_tree'] = Catehelp::get_left_tree($name_template, $conditional_name_template, array('table' => 'cheers_main_advtypes', 'id' => 'chr_advtypeid', 'name' => 'chr_advtypename'));
        $content_data['product_ids'] = array();
        $content_data['is_conditional'] = FALSE;
        $catalog_opt_template = array(
            'name_box' => '<option value="{id}">{indentation}{name}</option>',
            'indentation' => '&nbsp;|&nbsp;&nbsp;'
        );
        $content_data['catalog_opt'] = Catehelp::tree_to_str(Catehelp::get_tree($catalog_opt_template, 0, 0, array('table' => 'cheers_main_advtypes', 'id' => 'chr_advtypeid', 'name' => 'chr_advtypename')));
        if ($_POST) {
         
            $current_time = time();
            $advs = ORM::factory('cheers_main_adv');
            $_POST['chr_createdate'] = $current_time;
            $advs->values($_POST);
            if ($advs->check()) {
                $advs->save();
                if ($advs->saved()) {

   $imagesfile_define = Session::instance()->get('imagesfile_define');

                    if (is_array($imagesfile_define)) {

                        foreach ($imagesfile_define as $key => $value) {
                            $advsimage = ORM::factory('cheers_main_advimage');
                            $advsimage->chr_imageid = $advs->chr_advsid;
                            $advsimage->chr_filepath = $value;
                            $advsimage->chr_filestype = 2;
                            $advsimage->chr_createdate = time();
                            if ($advsimage->check()) {
                                $advsimage->save();
                            }
                        }
                    }
                    if (Session::instance()->get('temp_imagefiles')) {
                        Session::instance()->delete('temp_imagefiles');
                    }

                    if (Session::instance()->get('imagesfile_define')) {
                        Session::instance()->delete('imagesfile_define');
                    }
                    Message::set('广告添加成功');
                } else {
                    Message::set('广告添加失败', 'error');
                }
            }
        }
        $content = View::factory('admin/advert/adv_add', $content_data)->render();
        $this->template = View::factory('admin/template')->set('content', $content);
    }

    /**
     * 修改广告位置
     */
    public function action_edit() {
        $name_template = '<a href="/admin/adv/list/{id}" title="{name}新闻列表">{name}</a>';
        $conditional_name_template = '<a href="/admin/adv/list/{id}" title="{name}新闻列表">[ {name} ]</a>';
        $content_data['catalog_tree'] = Catehelp::get_left_tree($name_template, $conditional_name_template, array('table' => 'cheers_main_advtypes', 'id' => 'chr_advtypeid', 'name' => 'chr_advtypename'));
        $content_data['product_ids'] = array();
        $content_data['is_conditional'] = FALSE;
        $catalog_opt_template = array(
            'name_box' => '<option value="{id}" {selected}>{indentation}{name}</option>',
            'indentation' => '&nbsp;|&nbsp;&nbsp;'
        );
        $advs_id = $this->request->param('id');
        $advs_detail = ORM::factory('cheers_main_adv', $advs_id);
        $content_data['catalog_opt'] = Catehelp::tree_to_str(Catehelp::get_tree($catalog_opt_template, 0, 0, array('table' => 'cheers_main_advtypes', 'id' => 'chr_advtypeid', 'name' => 'chr_advtypename'), $advs_detail->chr_advtypeid));
        $advs_images = ORM::factory('cheers_main_advimage')
                ->where('chr_imageid', '=', $advs_id)
                ->and_where('chr_filestype', '=', 2)
                ->find_all()
                ->as_array();
        if ($_POST) {
            $current_time = time();
            $advs = ORM::factory('cheers_main_adv', $advs_id);
            $_POST['chr_createdate'] = $current_time;
            $advs->values($_POST);
            if ($advs->check()) {
                $advs->save();
                if ($advs->saved()) {
                    $imagesfile_define = Session::instance()->get('imagesfile_define');
                    if (is_array($imagesfile_define)) {

                        foreach ($imagesfile_define as $key => $value) {
                            $advsimage = ORM::factory('cheers_main_advimage');
                            $advsimage->chr_imageid = $advs->chr_advsid;
                            $advsimage->chr_filepath = $value;
                            $advsimage->chr_filestype = 2;
                            $advsimage->chr_createdate = time();
                            if ($advsimage->check()) {
                                $advsimage->save();
                            }
                        }
                    }
                    if (Session::instance()->get('temp_imagefiles')) {
                        Session::instance()->delete('temp_imagefiles');
                    }

                    if (Session::instance()->get('imagesfile_define')) {
                        Session::instance()->delete('imagesfile_define');
                    }
                    Message::set('广告修改成功');
                    Request::current()->redirect('admin/adv/edit/' . $advs_id);
                } else {
                    Message::set('广告修改失败', 'error');
                    Request::current()->redirect('admin/adv/edit/' . $advs_id);
                }
            }
        }
        $content = View::factory('admin/advert/adv_edit', $content_data)
                ->set('data', $advs_detail)
                ->set('advimages', $advs_images)
                ->render();
        $this->template = View::factory('admin/template')->set('content', $content);
    }

    /**
     * 删除广告位置
     */
    public function action_delete() {
        $advid = $this->request->param('id');

        $adv = ORM::factory('cheers_main_adv', $advid);

        if ($adv->loaded()) {

            $adv->delete($advid);

            Message::set('广告删除成功!');
            Request::current()->redirect('/admin/adv/list/');
        } else {
            Message::set('广告删除失败!', 'error');
            Request::current()->redirect('/admin/adv/list/');
        }
    }


}

?>
